package com.common.base.widget

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.common.base.R


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2023/2/5
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class KeyValueView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) :
    LinearLayout(context, attrs, defStyle) {
    private var valueTxt: TextView? = null
    private var keyTxt: TextView? = null
    private var keyHintTxt: TextView? = null
    private var image: ImageView? = null
    private var key: String?
    private var kvKeySize: Int
    private var keyHint: String?
    private var value: String?
    private var valueHint: String?
    private var defValue: String?
    private var imageId: Int
    private var bgResId: Int
    private var hasIcon = true
    private var location: Int
    private var keyHintSize: Int
    private var kvValueSize: Int
    private var kvKeyColor: Int
    private var mValueColor: Int
    private var mValueHintColor: Int
    private var kvDefValueColor: Int
    private var hasLink = true
    private var hasDpLink = false
    private var kvKeyStyle: Int
    private var kvValueStyle: Int
    private var keyHintColor: Int
    private var iconVisibility: Int = 0
    private var kvRightIconMarginRight: Int = 0
    private var image3: ImageView? = null
    private var image3Icon: Int
    private var kvValueLeftIconPadding: Int = 0
    private var kvValueLeftIconVisibility: Int = -1
    private var kvImageMarginRight: Int = 0
    private var kvImageWidth: Int = 35
    private var rightImageId: Int
    private var ckNotCopyClick = false


    init {
        val typed = context.obtainStyledAttributes(
            attrs,
            R.styleable.KeyValueView
        )
        key = typed.getString(R.styleable.KeyValueView_kvKey)
        kvKeySize = typed.getDimensionPixelSize(R.styleable.KeyValueView_kvKeySize, -1)
        keyHint = typed.getString(R.styleable.KeyValueView_kvKeyHint)
        location = typed.getInt(R.styleable.KeyValueView_kvValueAlign, LOCATION_RIGHT)
        value = typed.getString(R.styleable.KeyValueView_kvValue)
        valueHint = typed.getString(R.styleable.KeyValueView_kvValueHint)
        defValue = typed.getString(R.styleable.KeyValueView_kvDefValue)
        bgResId = typed.getResourceId(R.styleable.KeyValueView_kvBg, NOT_FOUND)
        imageId = typed.getResourceId(R.styleable.KeyValueView_kvImage, NOT_FOUND)
        image3Icon = typed.getResourceId(R.styleable.KeyValueView_kvValueLeftIcon, NOT_FOUND)
        //        hasIcon = typed.getBoolean(R.styleable.KeyValueView_kvHasIcon, hasIcon);
        keyHintSize = typed.getDimensionPixelSize(R.styleable.KeyValueView_kvKeyHintSize, -1)
        keyHintColor =
            typed.getColor(R.styleable.KeyValueView_kvKeyHintColor, Color.parseColor("#E3E3E3"))
        kvValueSize = typed.getDimensionPixelSize(R.styleable.KeyValueView_kvValueSize, -1)
        kvKeyColor = typed.getColor(R.styleable.KeyValueView_kvKeyColor, -1)
        mValueColor = typed.getColor(R.styleable.KeyValueView_kvValueColor, -1)
        mValueHintColor = typed.getColor(R.styleable.KeyValueView_kvValueHintColor, -1)
        kvDefValueColor = typed.getColor(R.styleable.KeyValueView_kvDefValueColor, -1)
        hasLink = typed.getBoolean(R.styleable.KeyValueView_kvHasLink, hasLink)
        hasDpLink = typed.getBoolean(R.styleable.KeyValueView_kvHasDpLink, hasDpLink)
        kvKeyStyle = typed.getInt(
            R.styleable.KeyValueView_kvKeyStyle,
            TEXT_STYLE_NORMAL
        )
        // 文字样式 正常|加粗
        kvValueStyle = typed.getInt(
            R.styleable.KeyValueView_kvValueStyle,
            TEXT_STYLE_NORMAL
        )
        iconVisibility = typed.getInt(
            R.styleable.KeyValueView_kvIconVisibility,
            iconVisibility
        )
        kvValueLeftIconVisibility = typed.getInt(
            R.styleable.KeyValueView_kvValueLeftIconVisibility,
            kvValueLeftIconVisibility
        )

        kvRightIconMarginRight = typed.getDimensionPixelSize(
            R.styleable.KeyValueView_kvRightIconMarginRight,
            kvRightIconMarginRight
        )
        kvValueLeftIconPadding= typed.getDimensionPixelSize(
            R.styleable.KeyValueView_kvValueLeftIconPadding,
            kvValueLeftIconPadding
        )
        kvImageWidth= typed.getDimensionPixelSize(
            R.styleable.KeyValueView_kvImageWidth,
            kvImageWidth
        )
        kvImageMarginRight= typed.getDimensionPixelSize(
            R.styleable.KeyValueView_kvImageMarginRight,
            kvImageMarginRight
        )
        rightImageId = typed.getResourceId(R.styleable.KeyValueView_kvRightImage, NOT_FOUND)

        ckNotCopyClick = typed.getBoolean(R.styleable.KeyValueView_kvNotCopyClick, false)

        typed.recycle()
        initView(context)
    }

    private fun initView(ctx: Context) {
        val inflater = LayoutInflater.from(ctx)
        val contentView: View = inflater.inflate(R.layout.base_view_key_value, null)
        this.addView(contentView)
        keyTxt = contentView.findViewById<View>(R.id.item_key) as TextView
        image = contentView.findViewById<View>(R.id.item_image) as ImageView
        image3 = contentView.findViewById<View>(R.id.item_icon_3) as ImageView
        if (kvKeySize != -1) {
            keyTxt?.setTextSize(TypedValue.COMPLEX_UNIT_PX, kvKeySize.toFloat())
        }
        setKey(key)
        if (!TextUtils.isEmpty(keyHint)) {
            keyHintTxt = contentView.findViewById<View>(R.id.item_key_hint) as TextView
            if (keyHintSize != -1) {
                keyHintTxt?.setTextSize(TypedValue.COMPLEX_UNIT_PX, keyHintSize.toFloat())
            }
            keyHintTxt?.visibility = VISIBLE
            keyHintTxt?.text = keyHint
            keyHintTxt?.setTextColor(keyHintColor)
        }
        valueTxt = contentView.findViewById<View>(R.id.item_value) as TextView
        if (location == LOCATION_LEFT) {
            val lp =
                RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.item_key)
            lp.leftMargin = resources.getDimensionPixelOffset(R.dimen.dp_10)
            valueTxt?.layoutParams = lp
        }
        if (location == LOCATION_TOP) {
            iconTop()
        }
        if (kvValueSize != -1) {
            valueTxt?.setTextSize(TypedValue.COMPLEX_UNIT_PX, kvValueSize.toFloat())
        }
        setValue(value)
        setValueHint(valueHint)
        if (kvValueStyle == TEXT_STYLE_BOLD) {
            valueTxt?.paint?.isFakeBoldText = true
        }
        if (!TextUtils.isEmpty(defValue)) {
            valueTxt?.hint = defValue
            if (kvDefValueColor != -1) {
                valueTxt?.setHintTextColor(kvDefValueColor)
            }
        }
        valueTxt?.setTextIsSelectable(ckNotCopyClick)

        if (bgResId != NOT_FOUND) {
            contentView.setBackgroundResource(bgResId)
        }
        if (imageId != NOT_FOUND) {
            image?.setImageResource(imageId)
            contentView.findViewById<View>(R.id.item_image).visibility = VISIBLE
        }
        if (rightImageId != NOT_FOUND) {
            contentView.findViewById<ImageView>(R.id.item_icon).setImageResource(rightImageId)
        }

        if (image3Icon != NOT_FOUND) {
            image3?.setImageResource(image3Icon)
        }
        when (iconVisibility) {
            1 -> contentView.findViewById<View>(R.id.item_icon).visibility =
                INVISIBLE
            2 -> contentView.findViewById<View>(R.id.item_icon).visibility = GONE
            else -> contentView.findViewById<View>(R.id.item_icon).visibility = VISIBLE
        }
        when (kvValueLeftIconVisibility) {
            1 -> contentView.findViewById<View>(R.id.item_icon_3).visibility =
                INVISIBLE
            0 -> contentView.findViewById<View>(R.id.item_icon_3).visibility = VISIBLE
            else -> contentView.findViewById<View>(R.id.item_icon_3).visibility = GONE
        }

        if (!hasLink) {
            contentView.findViewById<View>(R.id.body_total_rl).setBackgroundColor(
                resources.getColor(R.color.transparent)
            )
        } else {
            contentView.findViewById<View>(R.id.body_total_rl).background =
                resources.getDrawable(R.drawable.bottom_line_bg)
        }
        if (!hasDpLink) {
            contentView.findViewById<View>(R.id.bot_dp_link).visibility = GONE
        } else {
            contentView.findViewById<View>(R.id.bot_dp_link).visibility = VISIBLE
        }
        if (kvRightIconMarginRight > 0) {
            val params = LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            params.rightMargin = kvRightIconMarginRight
            findViewById<View>(R.id.item_icon).layoutParams = params
        }

            (findViewById<View>(R.id.item_image).layoutParams as ViewGroup.MarginLayoutParams).apply {
                width = kvImageWidth
//                height = newHeight
                // 设置margin
                rightMargin = kvImageMarginRight
            }

        if (kvValueLeftIconPadding > 0) {
            findViewById<View>(R.id.item_icon_3).setPadding(kvValueLeftIconPadding,kvValueLeftIconPadding,kvValueLeftIconPadding,kvValueLeftIconPadding)
        }

    }

    fun setKeyStyle(mStyle: Int) {
        when (mStyle) {
            TEXT_STYLE_BOLD -> keyTxt?.paint?.isFakeBoldText = true
            else -> keyTxt?.paint?.isFakeBoldText = false
        }
    }

    fun setKey(key: String?) {
        if (!TextUtils.isEmpty(key)) {
            keyTxt?.text = key
            if (kvKeyColor != -1) {
                keyTxt?.setTextColor(kvKeyColor)
            }
            if (kvKeyStyle == TEXT_STYLE_BOLD) {
                keyTxt?.paint?.isFakeBoldText = true
            }
        }
    }

    fun setKeyHint(value: String?) {
        if (!TextUtils.isEmpty(value)) {
            keyHintTxt?.text = value
        }
    }

    fun setValue(value: String?) {
        valueTxt?.text = value
        if (mValueColor != -1) {
            valueTxt?.setTextColor(mValueColor)
        }
    }

    fun setValueColor(value: Int) {
        if (value != -1) {
            mValueColor = value
            valueTxt?.setTextColor(mValueColor)
        }
    }

    fun setValueHint(value: String?) {
        valueTxt?.hint = value
        if (mValueHintColor != -1) {
            valueTxt?.setHintTextColor(mValueHintColor)
        }
    }

    fun getValue(): String {
        return valueTxt?.text.toString()
    }

    fun setValue(value: String?, textColor: Int) {
        valueTxt?.text = value
        valueTxt?.setTextColor(context.resources.getColor(textColor))
    }

    fun getImage(): ImageView? {
        return image
    }
    fun getValueLeftIcon(): ImageView? {
        return image3
    }

    /**
     * 图标向右
     */
    fun iconRight() {
        findViewById<View>(R.id.item_icon).visibility = VISIBLE
        findViewById<View>(R.id.item_icon_2).visibility = GONE
    }

    /**
     * 图标向下
     */
    fun iconDown() {
        findViewById<View>(R.id.item_icon_2).visibility = VISIBLE
        findViewById<View>(R.id.item_icon).visibility = GONE
    }

    /**
     * 图标向上
     */
    fun iconTop() {
        findViewById<View>(R.id.item_icon_2).visibility = VISIBLE
        findViewById<View>(R.id.item_icon).visibility = INVISIBLE
    }

    /**
     * 显示标点
     */
    fun showMark() {
        findViewById<View>(R.id.item_mark_view).visibility = VISIBLE
    }

    /**
     * 隐藏标点
     */
    fun hideMark() {
        findViewById<View>(R.id.item_mark_view).visibility = GONE
    }

    companion object {
        // 值的位置
        const val LOCATION_LEFT = 0
        const val LOCATION_RIGHT = 1
        const val LOCATION_TOP = 2

        /**
         * 文字样式 正常|加粗
         */
        const val TEXT_STYLE_NORMAL = 0
        const val TEXT_STYLE_BOLD = 1
        const val NOT_FOUND = -1
    }
}